package com.mooc.apigateway.controller;


import com.mooc.apigateway.common.CommonConstants;
import com.mooc.apigateway.common.ResultMsg;
import com.mooc.apigateway.common.UserContext;
import com.mooc.apigateway.common.page.PageData;
import com.mooc.apigateway.common.page.PageParams;
import com.mooc.apigateway.model.Comment;
import com.mooc.apigateway.model.House;
import com.mooc.apigateway.model.User;
import com.mooc.apigateway.model.UserMsg;
import com.mooc.apigateway.service.AgencyService;
import com.mooc.apigateway.service.CityService;
import com.mooc.apigateway.service.CommentService;
import com.mooc.apigateway.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class HouseController {

    @Autowired
    private HouseService houseService;

    @Autowired
    private CityService cityService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private AgencyService agencyService;

    //房产添加页面
    @RequestMapping("/house/toAdd")
    public String toAdd(ModelMap modelMap) {
        modelMap.put("citys", cityService.getAllCitys());
        modelMap.put("communitys", houseService.getAllCommunitys());
        //modelMap.put("citys","11");
        //modelMap.put("communitys","22");
        return "/house/add";
    }

    //添加房产页面功能
    @RequestMapping("/house/add")
    public String toAdd(House house){
        User user = UserContext.getUser();
        house.setState(CommonConstants.HOUSE_STATE_UP);
        houseService.addHouse(house,user);
        return "redirect:/house/ownlist?" + ResultMsg.successMsg("添加成功").asUrlParams();
    }

   //房屋列表(返回用户设置的房屋信息)
    @RequestMapping("/house/ownlist")
    public String ownlist(House house,Integer pageNum,Integer pageSize,ModelMap modelMap){
        User user = UserContext.getUser();
        house.setUserId(user.getId());
        house.setBookmarked(false);
        PageData<House> housePageData = houseService.queryHouse(house, PageParams.build(pageSize, pageNum));
        modelMap.put("ps",housePageData);
        modelMap.put("pageType","own");
        return "/house/ownlist";
    }


    /*
      房屋详情
      1.房屋详情，读取时同时要设置房屋热度加1
     */
    @RequestMapping("/house/detail")
    public String detail(Long id,ModelMap map){

        //1.设置房屋详情信息
         House house= houseService.getDetailById(id);
         map.put("house",house);
        //2.设置推荐房产
        List<House> houses = houseService.queryRecommdHouses(4);
        map.put("recomHouses",houses);
        //3.房屋对应房屋代理人信息
        User agent = houseService.querySaleHouseAgent(house.getId());
        map.put("agent",agent);
        //4.得到该房屋的评论列表
        List<Comment> commentList = commentService.getHouseComments(house.getId());
        map.put("commentList",commentList);

        return "/house/detail";
    }


    /*
      房屋收藏
     */
   @RequestMapping("/house/bookmark")
    public ResultMsg bookMark(Integer id){
       houseService.bookMark(id);
       return ResultMsg.successMsg("ok");
   }

   @RequestMapping("/house/unbookmark")
    public ResultMsg unbookmark(Integer id){
       houseService.unbookmark(id);
       return ResultMsg.successMsg("ok");
   }

    /**
     * 1.实现分页
     * 2.支持小区搜索、类型搜索
     * 3.支持排序图片、价格、标题、地址等信息
     * 4.支持展示
     * @return
     */
    @RequestMapping("/house/list")
    public String houseList(Integer pageSize,Integer pageNum,House query,ModelMap modelMap){
        //得到房屋列表
        PageData<House> ps =  houseService.queryHouse(query,PageParams.build(pageSize, pageNum));
        //2.设置推荐房产
        List<House> hotHouses = houseService.queryRecommdHouses(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("ps", ps);
        modelMap.put("vo", query);
        return "house/listing";
    }


    @RequestMapping("house/leaveMsg")
    public String houseMsg(UserMsg userMsg){
        houseService.addUserMsg(userMsg);
       // agencyService.sendUserMail(userMsg);
        return "redirect:/house/detail?id=" + userMsg.getHouseId() + ResultMsg.successMsg("留言成功").asUrlParams();
    }


}
